import cv2
import numpy as np

def draw_yolo_bboxes_on_image(image_path, bbox_txt_path, output_path):
    # 读取图片
    image = cv2.imread(image_path)
    height, width, _ = image.shape

    # 读取 YOLO 格式的 bbox txt 文件
    with open(bbox_txt_path, 'r') as f:
        lines = f.readlines()

    for line in lines:
        # 解析每行数据
        data = line.strip().split()
        class_id = int(data[0])
        x_center = float(data[1])
        y_center = float(data[2])
        box_width = float(data[3])
        box_height = float(data[4])

        # 将 YOLO 格式的坐标转换为像素坐标
        x1 = int((x_center - box_width / 2) * width)
        y1 = int((y_center - box_height / 2) * height)
        x2 = int((x_center + box_width / 2) * width)
        y2 = int((y_center + box_height / 2) * height)

        # 绘制边界框
        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
        # 绘制类别标签
        cv2.putText(image, str(class_id), (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

    # 保存处理后的图片
    cv2.imwrite(output_path, image)

# 使用示例
# image_path = 'synbboxpcb/image/pcb_0000.png'
image_path='data_zoo/splitsynbboxpcb/image/pcb_0000_0_922_1024_1024_102.png'
# bbox_txt_path = 'synbboxpcb/cpbbox/pcb_0000_cpbbox.txt'
bbox_txt_path="data_zoo/splitsynbboxpcb/cpbbox/pcb_0000_0_922_1024_1024_102.txt"
output_path = 'checkcpbbox.jpg'
draw_yolo_bboxes_on_image(image_path, bbox_txt_path, output_path)
    